﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.ISV.PM;
using UFIDA.U9.PPR.PurPriceAdjustment;
using UFSoft.UBF.Business;

namespace ufida.u9.ait.openapi
{
    internal class PurPriceAdjustmentBase
    {
        public static PurPriceAdjustment ApproveDoc(PurPriceAdjustment doc)
        {
            if (doc.Status == UFIDA.U9.PPR.Enums.Status.Approved)
                throw new Exception($"单号[{doc.DocNo}]状态为已审");

            ApprovedPurPriceAdjustmentSRV sv = new ApprovedPurPriceAdjustmentSRV();
            sv.PurPriceAdjustments = new List<UFIDA.U9.Base.DTOs.IDCodeNameDTO>()
            {
                new UFIDA.U9.Base.DTOs.IDCodeNameDTO()
                {
                    ID=doc.ID
                }
            };
            sv.Do();

            return doc;
        }

        public static PurPriceAdjustment SubmitDoc(PurPriceAdjustment doc)
        {
            if (doc.Status != UFIDA.U9.PPR.Enums.Status.Opened)
                throw new Exception($"单号[{doc.DocNo}]状态不为开立");

            SubmitPurPriceAdjustmentSRV sv = new SubmitPurPriceAdjustmentSRV();
            sv.PurPriceAdjustments = new List<UFIDA.U9.Base.DTOs.IDCodeNameDTO>()
            {
                new UFIDA.U9.Base.DTOs.IDCodeNameDTO()
                {
                    ID = doc.ID
                }
            };
            sv.Do();

            return doc;
        }

        public static PurPriceAdjustment RevocateDoc(PurPriceAdjustment doc)
        {
            if(doc.Status!= UFIDA.U9.PPR.Enums.Status.Approving)
                throw new Exception($"单号[{doc.DocNo}]状态不为提交");

            using(ISession session = Session.Open())
            {
                doc.Status = UFIDA.U9.PPR.Enums.Status.Opened;

                session.Commit();
            }

            return doc;
        }
    }
}
